//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.wi.group;

import java.awt.*;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.*;
import org.jpgolay.gui.iconBar.IconBar;

public class GroupGUI extends JInternalFrame {

    private class TableModel extends AbstractTableModel {

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public boolean isCellEditable(int row, int col) {
            if (col == 0) {
                return false;
            } else {
                return cellEditable;
            }
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }

        TableModel() {
        }
    }

    public GroupGUI() {
        cellEditable = false;
        initComponents();
    }

    private void initComponents() {
        iconBar1 = new IconBar();
        mainP = new JPanel();
        groupTP = new JTabbedPane();
        groupP = new JPanel();
        groupSP = new JScrollPane();
        group = new JTable();
        jMenuBar1 = new JMenuBar();
        optionsM = new JMenu();
        newMI = new JMenuItem();
        openMI = new JMenuItem();
        saveMI = new JMenuItem();
        modifyMI = new JMenuItem();
        removeMI = new JMenuItem();
        jSeparator1 = new JSeparator();
        printMI = new JMenuItem();
        helpMI = new JMenuItem();
        setFrameIcon(new ImageIcon(getClass().getResource("/org/jpgolay/wi/images/logo.gif")));
        setTitle(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("group_title"));
        iconBar1.setEnabledPrint(false);
        iconBar1.setEnabledCreate(false);
        iconBar1.setEnabledModify(false);
        iconBar1.setEnabledRemove(false);
        iconBar1.setEnabledSave(false);
        getContentPane().add(iconBar1, "North");
        mainP.setLayout(new BorderLayout());
        mainP.setBorder(new BevelBorder(1));
        groupTP.setTabLayoutPolicy(1);
        groupP.setLayout(new BorderLayout());
        groupP.setBorder(new LineBorder(new Color(255, 153, 0), 2));
        group.setModel(new TableModel());
        group.getColumnModel().getColumn(0).setPreferredWidth(20);
        group.getColumnModel().getColumn(1).setPreferredWidth(100);
        group.getColumnModel().getColumn(2).setPreferredWidth(100);
        group.getColumnModel().getColumn(3).setPreferredWidth(100);
        groupSP.setViewportView(group);
        groupP.add(groupSP, "Center");
        groupTP.addTab(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("groups"), new ImageIcon(getClass().getResource("/org/jpgolay/wi/images/orange.png")), groupP);
        mainP.add(groupTP, "Center");
        getContentPane().add(mainP, "Center");
        optionsM.setMnemonic('O');
        optionsM.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Options"));
        newMI.setAccelerator(KeyStroke.getKeyStroke(78, 2));
        newMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/new.png")));
        newMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("new"));
        newMI.setEnabled(false);
        optionsM.add(newMI);
        openMI.setAccelerator(KeyStroke.getKeyStroke(79, 2));
        openMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/open.png")));
        openMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("open"));
        optionsM.add(openMI);
        saveMI.setAccelerator(KeyStroke.getKeyStroke(83, 2));
        saveMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/save.png")));
        saveMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("save"));
        saveMI.setEnabled(false);
        optionsM.add(saveMI);
        modifyMI.setAccelerator(KeyStroke.getKeyStroke(77, 2));
        modifyMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/modify.png")));
        modifyMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("modify"));
        modifyMI.setEnabled(false);
        optionsM.add(modifyMI);
        removeMI.setAccelerator(KeyStroke.getKeyStroke(88, 2));
        removeMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/delete.png")));
        removeMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("delete"));
        removeMI.setEnabled(false);
        optionsM.add(removeMI);
        optionsM.add(jSeparator1);
        printMI.setAccelerator(KeyStroke.getKeyStroke(80, 2));
        printMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/printer.png")));
        printMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("print"));
        printMI.setEnabled(false);
        optionsM.add(printMI);
        helpMI.setAccelerator(KeyStroke.getKeyStroke(65, 2));
        helpMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/help.png")));
        helpMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("help"));
        optionsM.add(helpMI);
        jMenuBar1.add(optionsM);
        setJMenuBar(jMenuBar1);
        pack();
    }

    public JTable getGroup() {
        return group;
    }

    public JMenuItem getHelpMI() {
        return helpMI;
    }

    public IconBar getIconBar1() {
        return iconBar1;
    }

    public JMenuItem getModifyMI() {
        return modifyMI;
    }

    public JMenuItem getNewMI() {
        return newMI;
    }

    public JMenuItem getOpenMI() {
        return openMI;
    }

    public JMenuItem getPrintMI() {
        return printMI;
    }

    public JMenuItem getRemoveMI() {
        return removeMI;
    }

    public JMenuItem getSaveMI() {
        return saveMI;
    }

    public void setGroup(JTable table) {
        group = table;
    }

    public boolean isCellEditable() {
        return cellEditable;
    }

    public void setCellEditable(boolean cellEditable) {
        this.cellEditable = cellEditable;
    }

    public Object[][] getData() {
        return data;
    }

    public void setData(Object data[][]) {
        this.data = data;
        group.setModel(new TableModel());
        group.getColumnModel().getColumn(0).setPreferredWidth(20);
        group.getColumnModel().getColumn(1).setPreferredWidth(100);
        group.getColumnModel().getColumn(2).setPreferredWidth(100);
        group.getColumnModel().getColumn(3).setPreferredWidth(100);
        groupSP.setViewportView(group);
    }

    private void initTable() {
        data = new Object[5][4];
        for (int i = 0; i < 5; i++) {
            data[i][0] = "";
            data[i][1] = "";
            data[i][2] = "";
            data[i][3] = "";
        }

        setData(data);
    }

    public void initFields() {
        cellEditable = false;
        initTable();
    }
    private JTable group;
    private JPanel groupP;
    private JScrollPane groupSP;
    private JTabbedPane groupTP;
    private JMenuItem helpMI;
    private IconBar iconBar1;
    private JMenuBar jMenuBar1;
    private JSeparator jSeparator1;
    private JPanel mainP;
    private JMenuItem modifyMI;
    private JMenuItem newMI;
    private JMenuItem openMI;
    private JMenu optionsM;
    private JMenuItem printMI;
    private JMenuItem removeMI;
    private JMenuItem saveMI;
    private Object data[][] = {
        {
            "", "", "", ""
        }, {
            "", "", "", ""
        }, {
            "", "", "", ""
        }, {
            "", "", "", ""
        }, {
            "", "", "", ""
        }
    };
    private String columnNames[] = {
        "ID", "NOM", "ROLE", "NOTE"
    };
    private boolean cellEditable;
}
